using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectTinTuc()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectTinTucPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListTinTuc", CreateViewModel(data));
		}

		public ActionResult SelectTinTucByLoaiTinTuc(int idloaitintuc, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiTinTuc = idloaitintuc,
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return View("SelectTinTuc", CreateViewModel(data));
		}

		public ActionResult SelectTinTucByNguoiDang(Guid idnguoidang, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDang = idnguoidang,
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				CurrentPage = page
			};
			return View("SelectTinTuc", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteTinTuc(int id)
		{
			var deleteItem = _repTinTuc.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idloaitintuc = deleteItem.IDLoaiTinTuc;
			var idnguoidang = deleteItem.IDNguoiDang;
			var result = _repTinTuc.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repTinTuc.GetTotalItemByLoaiTinTuc(idloaitintuc),
				DeleteId = id
			});
		}

		public ActionResult DetailOfTinTuc(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_TINTUC,
				EditID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateTinTuc(FormCollection forms)
		{
			string id = forms.GetValues("TinTuc_ID")[0];
			string idloaitintuc = forms.GetValues("TinTuc_IDLoaiTinTuc")[0];
			string idnguoidang = forms.GetValues("TinTuc_IDNguoiDang")[0];
			string tieude = forms.GetValues("TinTuc_TieuDe")[0];
			string noidung = forms.GetValues("TinTuc_NoiDung")[0];
			string noidungrutgon = forms.GetValues("TinTuc_NoiDungRutGon")[0];
			var file = Request.Files["TinTuc_HinhMinhHoa"];
			string refName = "";
			var editItem = _repTinTuc.SelectByID(int.Parse(id));
			editItem.IDLoaiTinTuc = int.Parse(idloaitintuc);
			editItem.IDNguoiDang = Guid.Parse(idnguoidang);
			editItem.TieuDe = tieude;
			editItem.NoiDung = noidung;
			editItem.NoiDungRutGon = noidungrutgon;
            var result = false;
            if (file.ContentLength != 0)
            {
                result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
                editItem.HinhMinhHoa = refName;
            }
            result = _repTinTuc.Save();
			var data = new DataTransferViewModel
			{
                IDLoaiTinTuc = int.Parse(idloaitintuc),
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_TINTUC,
				EditID = int.Parse(id),
				Updated = result,
			};
			return View("DetailOfTinTuc", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertTinTuc(FormCollection forms)
		{
			string idloaitintuc = forms.GetValues("TinTuc_IDLoaiTinTuc")[0];
			string idnguoidang = forms.GetValues("TinTuc_IDNguoiDang")[0];
			string tieude = forms.GetValues("TinTuc_TieuDe")[0];
			string noidung = forms.GetValues("TinTuc_NoiDung")[0];
			string noidungrutgon = forms.GetValues("TinTuc_NoiDungRutGon")[0];
			string ngaydang = forms.GetValues("TinTuc_NgayDang")[0];
			var file = Request.Files["TinTuc_HinhMinhHoa"];
			string refName = "";
			var newItem = new TinTuc
			{
				IDLoaiTinTuc = int.Parse(idloaitintuc),
				IDNguoiDang = Guid.Parse(idnguoidang),
				TieuDe = tieude,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				NgayDang = DateTime.Parse(ngaydang),
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repTinTuc.Insert(newItem);
			if (result)
			{
                if (file.ContentLength != 0)
                {
                    result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
                    if (result)
                    {
                        var addedItem = _repTinTuc.SelectByID(newItem.ID);
                        addedItem.HinhMinhHoa = refName;
                        _repTinTuc.Save();
                    }
                }
			}
			var data = new DataTransferViewModel
			{
                IDLoaiTinTuc = int.Parse(idloaitintuc),
				EnumViewModelType = EnumViewModel.ADMIN_TINTUC,
				Added = result,
			};
			return View("SelectTinTuc", CreateViewModel(data));
		}


	}
}